package com.powerunion.datacollection.service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;

import com.powerunion.datacollection.common.Constants;
import com.powerunion.datacollection.common.SystemException;
import com.powerunion.datacollection.dao.DaoFactory;
import com.powerunion.datacollection.dao.ProcessInfoDao;
import com.powerunion.datacollection.model.ActivityInfo;
import com.powerunion.datacollection.model.jsonObject.JSONDataBean;
import com.powerunion.util.log.LogHelper;

public class ProcessInfoManager {
	private static Logger logger = LogHelper.getInstance().getLogger(
			ProcessInfoManager.class);
	private ProcessInfoDao dao = null;

	public ProcessInfoManager() {
		super();
		this.dao = (ProcessInfoDao) DaoFactory.getInstance().getDao(
				ProcessInfoDao.class);
	}

	public boolean save(String saveActivityInfoStr, String currentUser,String saveItemInfoStr)  throws Exception {
		// 参数为空
		if (StringUtils.isEmpty(saveActivityInfoStr)) {
			String[] args = { "saveActivityInfoStr" };
			// 打印错误日志
			StringBuffer msglog = new StringBuffer(
					"empty invoke parameter saveActivityInfoStr NULL ");
			logger.error(msglog.toString());
			throw new SystemException("error.common.parameter.empty", args);
		}
		
		// 参数为空
		if (StringUtils.isEmpty(currentUser)) {
			String[] args = { "currentUser" };
			// 打印错误日志
			StringBuffer msglog = new StringBuffer(
					"empty invoke parameter currentUser NULL ");
			logger.error(msglog.toString());
			throw new SystemException("error.common.parameter.empty", args);
		}
		
		//参数为空
		if (StringUtils.isEmpty(saveItemInfoStr)) {
			String[] args = { "saveItemInfoStr" };
			// 打印错误日志
			StringBuffer msglog = new StringBuffer(
					"empty invoke parameter saveItemInfoStr NULL ");	
			logger.error(msglog.toString());
			throw new SystemException("error.common.parameter.empty", args);
		}
logger.debug("saveActivityInfoStr:"+saveActivityInfoStr);
		ItemManager im = new ItemManager();
	    String itemId = im.save(saveItemInfoStr, currentUser, Constants.PROCESS);
	    //先把原先的流程信息删除
	    this.dao.delete(itemId);
	    UrgencyManager um = new UrgencyManager();
	    //删除催办信息
	    um.delete(itemId);
	    
		Map activityInfoMap = new HashMap();
		activityInfoMap.put("data", ActivityInfo.class);
	
		JSONDataBean am = (JSONDataBean)JSONObject.toBean(JSONObject.fromObject(saveActivityInfoStr), JSONDataBean.class,activityInfoMap);
		List list = am.getData();
		boolean flag = false;
		if(!list.isEmpty()){
			ActivityInfo activityInfo = (ActivityInfo)am.getData().get(0);
			flag = this.dao.save(activityInfo, itemId);
		}

		return flag;
	}
	
	public List getProcessInfo(String processId)throws Exception{
		// 参数为空
		if (StringUtils.isEmpty(processId)) {
			String[] args = { "processId" };
			// 打印错误日志
			StringBuffer msglog = new StringBuffer(
					"empty invoke parameter processId NULL ");
			logger.error(msglog.toString());
			throw new SystemException("error.common.parameter.empty", args);
		}
		
		return this.dao.getProcessInfo(processId);
	}
	
	public String validateActivityInfo(String procId) throws Exception {
		// 参数为空
		if (StringUtils.isEmpty(procId)) {
			String[] args = { "procId" };
			// 打印错误日志
			StringBuffer msglog = new StringBuffer(
					"empty invoke parameter procId NULL ");
			logger.error(msglog.toString());
			throw new SystemException("error.common.parameter.empty", args);
		}
		return this.dao.validateActivityInfo(procId);
	}
	
	public String getProcessInfoDetails(String workflowId,String procId) throws Exception {
		// 参数为空
		if (StringUtils.isEmpty(workflowId)) {
			String[] args = { "workflowId" };
			// 打印错误日志
			StringBuffer msglog = new StringBuffer(
					"empty invoke parameter workflowId NULL ");
			logger.error(msglog.toString());
			throw new SystemException("error.common.parameter.empty", args);
		}
		
		// 参数为空
		if (StringUtils.isEmpty(procId)) {
			String[] args = { "procId" };
			// 打印错误日志
			StringBuffer msglog = new StringBuffer(
					"empty invoke parameter procId NULL ");
			logger.error(msglog.toString());
			throw new SystemException("error.common.parameter.empty", args);
		}
		
		List details = this.dao.getProcessInfoDetails(workflowId,procId);
		JSONArray jsonArr = JSONArray.fromObject(details);
		String result = "{success:true,results:"+jsonArr+"}";
		return result;
	}
	
}
